package com.example.x.helpmeget2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import android.view.ViewGroup.LayoutParams;

public class OneFragment extends Fragment  implements View.OnClickListener {
        private ImageView[] imageViews = null;
        private ImageView imageView = null;
        private ViewPager advPager = null;
        private AtomicInteger what = new AtomicInteger(0);
        private boolean isContinue = true;

      View rootView;


        @Nullable

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                 rootView =  inflater.inflate(R.layout.frame_main,container,false);


                initViewPager();

            //    rootView.findViewById(R.id.button0).setOnClickListener(this);
  //      rootView.findViewById(R.id.button1).setOnClickListener(this);
 //       rootView.findViewById(R.id.button2).setOnClickListener(this);

        return  rootView;
        }



                @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        }

        @Override
        public void onClick(View v) {

        }

        private void whatOption() {
                what.incrementAndGet();
                if (what.get() > imageViews.length - 1) {
                        what.getAndAdd(-4);
                }
                try {
                        Thread.sleep(5000);
                } catch (InterruptedException e) {

                }
        }
        private void initViewPager() {
                advPager = (ViewPager)rootView.findViewById(R.id.adv_pager);
                ViewGroup group = (ViewGroup) rootView.findViewById(R.id.viewGroup);

//      �������ŵ������Ź��汳��
                List<View> advPics = new ArrayList<View>();

                ImageView img1 = new ImageView(getContext());
                img1.setBackgroundResource(R.drawable.six_bg);
                advPics.add(img1);

                ImageView img2 = new ImageView(getContext());
                img2.setBackgroundResource(R.drawable.six_bg);
                advPics.add(img2);

                ImageView img3 = new ImageView(getContext());
                img3.setBackgroundResource(R.drawable.six_bg);
                advPics.add(img3);

                ImageView img4 = new ImageView(getContext());
                img4.setBackgroundResource(R.drawable.six_bg);
                advPics.add(img4);

//      ��imageviews��������
                imageViews = new ImageView[advPics.size()];
//Сͼ��
                for (int i = 0; i < advPics.size(); i++) {
                        imageView = new ImageView(getContext());
                        imageView.setLayoutParams(new LayoutParams(20, 20));
                        imageView.setPadding(5, 5, 5, 5);
                        imageViews[i] = imageView;
                        if (i == 0) {
                                imageViews[i]
                                        .setBackgroundResource(R.drawable.banner_dian_focus);
                        } else {
                                imageViews[i]
                                        .setBackgroundResource(R.drawable.banner_dian_blur);
                        }
                        group.addView(imageViews[i]);
                }

                advPager.setAdapter(new AdvAdapter(advPics));
                advPager.setOnPageChangeListener(new GuidePageChangeListener());
                advPager.setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                switch (event.getAction()) {
                                        case MotionEvent.ACTION_DOWN:
                                        case MotionEvent.ACTION_MOVE:
                                                isContinue = false;
                                                break;
                                        case MotionEvent.ACTION_UP:
                                                isContinue = true;
                                                break;
                                        default:
                                                isContinue = true;
                                                break;
                                }
                                return false;
                        }
                });
                new Thread(new Runnable() {

                        @Override
                        public void run() {
                                while (true) {
                                        if (isContinue) {
                                                viewHandler.sendEmptyMessage(what.get());
                                                whatOption();
                                        }
                                }
                        }

                }).start();
        }

        private final Handler viewHandler = new Handler() {

                @Override
                public void handleMessage(Message msg) {
                        advPager.setCurrentItem(msg.what);
                        super.handleMessage(msg);
                }

        };

        private final class GuidePageChangeListener implements ViewPager.OnPageChangeListener {

                @Override
                public void onPageScrollStateChanged(int arg0) {

                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {

                }

                @Override
                public void onPageSelected(int arg0) {
                        what.getAndSet(arg0);
                        for (int i = 0; i < imageViews.length; i++) {
                                imageViews[arg0]
                                        .setBackgroundResource(R.drawable.banner_dian_focus);
                                if (arg0 != i) {
                                        imageViews[i]
                                                .setBackgroundResource(R.drawable.banner_dian_blur);
                                }
                        }

                }

        }

        private final class AdvAdapter extends PagerAdapter {
                private List<View> views = null;

                public AdvAdapter(List<View> views) {
                        this.views = views;
                }

                @Override
                public void destroyItem(View arg0, int arg1, Object arg2) {
                        ((ViewPager) arg0).removeView(views.get(arg1));
                }

                @Override
                public void finishUpdate(View arg0) {

                }

                @Override
                public int getCount() {
                        return views.size();
                }

                @Override
                public Object instantiateItem(View arg0, int arg1) {
                        ((ViewPager) arg0).addView(views.get(arg1), 0);
                        return views.get(arg1);
                }

                @Override
                public boolean isViewFromObject(View arg0, Object arg1) {
                        return arg0 == arg1;
                }

                @Override
                public void restoreState(Parcelable arg0, ClassLoader arg1) {

                }

                @Override
                public Parcelable saveState() {
                        return null;
                }

                @Override
                public void startUpdate(View arg0) {

                }

        }
}